using System;
using System.Collections.Generic;
using System.Text;
using CodePlex.Resourceful.Amazon.S3;
using System.IO;

namespace CodePlex.Resourceful.CommandLine.Actions.S3
{
    public class PutObjects : BaseS3
    {
        public PutObjects()
            : base("put-objects", "Creates or updates multiple S3 objects.", 
                ParameterInfo.Required("aws-key","AWS access key ID",DefaultAwsKey()),
                ParameterInfo.Required("aws-secret","AWS secret key",DefaultAwsSecret()),
                ParameterInfo.Required("bucket","S3 bucket"),
                ParameterInfo.Required("files", @"Local source files with a wildcard  e.g.  c:\*.txt"),

                ParameterInfo.Optional("key-prefix", "S3 destination key-prefix",string.Empty),

                ParameterInfo.Optional("overwrite", string.Format("Whether or not to overwrite an object if it exists, one of: {0}", FormatEnumValues<OverwriteMode>()), FromEnum(OverwriteMode.Never)),
                ParameterInfo.Optional("create-bucket","Whether or not to create the bucket if it does not exist",false),
                ParameterInfo.Optional("location", "If creating bucket, which datacenter to use (US or EU)", Datacenter.US.ToString()),    
                ParameterInfo.Optional("acl",string.Format("Canned acl policy, one of: {0}",Utils.Join(",",S3Constants.CannedAccessPolicies.All)),S3Constants.CannedAccessPolicies.Private)

            
            )
        {

        }



        private IEnumerable<string> Files(string files)
        {
            int starIndex = files.IndexOf('*');

            if (starIndex > -1) 
            {
                int lastDir = (files.Substring(0,starIndex)).LastIndexOf(Path.DirectorySeparatorChar);

                string dir = lastDir==-1?".":files.Substring(0,lastDir);

                string pattern = files.Substring(lastDir+1);

                return Directory.GetFiles(dir,pattern);



            }

            throw CodePlexException.Format("Expected a * in the files parameter. (e.g. *.jpg)");
        }

       

        protected override void GoS3(S3Connection connection, NameValuePairs parameters)
        {
            string bucket = parameters["bucket"];

            string files = parameters["files"];

            string keyPrefix = parameters.GetIfExists("key-prefix");

            OverwriteMode overwrite = GetOptional<OverwriteMode>(parameters, "overwrite");
            bool createBucket = GetOptional<bool>(parameters, "create-bucket");

            string acl = GetOptional<string>(parameters, "acl");
            Datacenter location = this.CheckLocation(GetOptional<string>(parameters, "location"));


            CheckAcl(acl);

            this.CreateBucket(connection, createBucket, bucket, location);

            foreach(string file in Files(files))
            {
                CheckFile(file);

 
                string key = (keyPrefix??string.Empty) + Path.GetFileName(file);

                this.PutObject(connection, overwrite, bucket,key,acl,file);
            }

           
        }
    }
}
